﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ShahyadS.TFSBuildInspectionModel;
using Microsoft.TeamFoundation.Common;
using System.Threading;
using System.Windows;
using Fasterflect;
namespace ShahyadS.BuildInspectorVSPackage
{
	public static class BuildDefinitionRowExtensions
	{
		static Dictionary<BuildDefinitionRow, BaseHierarchyNode> RowUIMap = new Dictionary<BuildDefinitionRow,BaseHierarchyNode>();
		private static void RefreshCallBack(IAsyncResult ar)
		{
			BuildDefinitionRow row = (ar.AsyncState as List<Object>)[1] as BuildDefinitionRow;
			string MethodName = (ar.AsyncState as List<Object>)[2] as string;
			BaseHierarchyNode node = (ar.AsyncState as List<Object>)[0] as BaseHierarchyNode;
			var z = node.GetChildNodesRecursively<BaseHierarchyNode>();
			var x = (from z1 in z where z1 is BaseHierarchyNode && (z1 as BaseHierarchyNode).Name.StartsWith(row.BuildDefinitionName) select z1).FirstOrDefault();
			if (x != null)
			{
				if (!RowUIMap.ContainsKey(row))
				{
					RowUIMap.Add(row, x);
				}
				if (MethodName == "Select")
				{
					x.Select();
				}
				else
				{
					x.ParentHierarchy.CallMethod(MethodName, x);
				}
			}

		}
		public static BaseHierarchyNode InvokeAction(this BuildDefinitionRow row, string MethodName)
		{
			IVsTeamExplorer VsTeamExplorer = BuildInspectorVSPackagePackage.GetGlobalService(typeof(IVsTeamExplorer)) as IVsTeamExplorer;
			if (VsTeamExplorer.TeamExplorerWindow == null || VsTeamExplorer.TeamExplorerUIHierarchy == null)
			{
				MessageBox.Show("Team Explorer Pane is not available.");
				return null;
			}
			if (!RowUIMap.ContainsKey(row) || RowUIMap[row] == null)
			{
				VsTeamExplorer.RefreshTeamExplorerView();
				var ddd = VsTeamExplorer.QuerySetCurrentServerAndProject(row.Parent.BuildServer.TeamFoundationServer.Uri.ToString(), row.TeamProject, row.Parent.VersionControlServer.GetTeamProject(row.TeamProject).ArtifactUri.ToString());
				VsTeamExplorer.SetCurrentServerAndProject(row.Parent.BuildServer.TeamFoundationServer.Uri.ToString(), row.TeamProject, row.Parent.VersionControlServer.GetTeamProject(row.TeamProject).ArtifactUri.ToString());
				//var y = buildExplorerPlugin.GetTeamProjectNode(row.TeamProject);

				var teamProjectNode = VsTeamExplorer.GetTeamProjectNode(row.TeamProject);
				if (teamProjectNode == null)
				{
					return null;
				}
				var rootNode = VsTeamExplorer.GetRootNode();
				IAsyncResult ar = rootNode.BeginRefresh(RefreshCallBack, new List<Object> {teamProjectNode, row, MethodName });
				var z = rootNode.GetChildNodesRecursively<BaseHierarchyNode>();
				var x = (from z1 in z where z1 is BaseHierarchyNode && (z1 as BaseHierarchyNode).Name.StartsWith(row.BuildDefinitionName) select z1).FirstOrDefault();
				if (x != null)
				{
					RowUIMap.Add(row, x);
				}
				return x;
			}
			if (MethodName == "Select")
			{
				RowUIMap[row].Select();
			}
			else
			{
				RowUIMap[row].ParentHierarchy.CallMethod(MethodName, RowUIMap[row]);
			}
			return RowUIMap[row];
		}

	}
}
